#!/bin/sh
# PCP QA Test No. 1478
# Workout for pcp-reboot-init - $PCP_VAR_DIR fixup part
#
# Copyright (c) 2024 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check


_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@$PCP_BINADM_DIR@PCP_BINADM_DIR@g" \
    # end
}

mkdir $tmp

# setup a "correct" NOTICES to avoid babble from trying to
# fix this ... note this test is using pcpqa for $PCP_USER and
# $PCP_GROUP
# Note: this is $tmp/log/NOTICES, not the real NOTICES file.
#
mkdir $tmp/log
$sudo chown pcpqa:pcpqa $tmp/log
export PCP_LOG_DIR=$tmp/log
$sudo touch "$PCP_LOG_DIR"/NOTICES
$sudo chown pcpqa:pcpqa "$PCP_LOG_DIR"/NOTICES

export PCP_DIR=$tmp
unset PCP_ENV_DONE
unset PCP_RUN_DIR
unset PCP_USER
unset PCP_GROUP

# real QA test starts here
echo "=== \$PCP_DIR borked"
$PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
mkdir $tmp/etc
cp /etc/pcp.env $tmp/etc/pcp.env
$PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter

echo
echo "=== missing vars from pcp.conf"
touch $tmp/etc/pcp.conf
echo "PCP_FOO=bar" >>$tmp/etc/pcp.conf
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
echo "PCP_USER=pcpqa" >>$tmp/etc/pcp.conf
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
echo "PCP_GROUP=pcpqa" >>$tmp/etc/pcp.conf
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
echo "PCP_RUN_DIR=$tmp/run" >>$tmp/etc/pcp.conf
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter

echo
echo "=== \$PCP_RUN_DIR missing"
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
ls -ld $tmp/run | _filter_ls -s | _filter

echo
echo "=== \$PCP_RUN_DIR exists and OK, so do nothing"
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
ls -ld $tmp/run | _filter_ls -s | _filter

echo
echo "=== \$PCP_RUN_DIR exists but various problems ..."
echo "--- wrong user"
iam=`id -u`
$sudo chown $iam:pcpqa $tmp/run
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
ls -ld $tmp/run | _filter_ls -s | _filter
echo "--- wrong group"
iam=`id -g`
$sudo chown pcpqa:$iam $tmp/run
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
ls -ld $tmp/run | _filter_ls -s | _filter
echo "--- wrong mode"
$sudo chmod 755 $tmp/run
$sudo $PCP_BINADM_DIR/pcp-reboot-init 2>&1 | _filter
ls -ld $tmp/run | _filter_ls -s | _filter

# success, all done
exit
